package cn.weiyang.interceptor;

import cn.weiyang.controller.Errcode;
import cn.weiyang.controller.ServiceException;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 会话处理拦截器（该拦截器纯粹是为了测试会话而写的，并无实际用处，测试其它类时可以先在springmvc.xml中先把有关它的配置先注释掉，以便测试其它类及方法）
 * Created by Administrator on 2017/7/26.
 */
public class SessionInterceptor implements HandlerInterceptor {
    /*利用log4j进行测试*/
    private static Logger logger= LogManager.getLogger(SessionInterceptor.class);

    public boolean preHandle(HttpServletRequest request, HttpServletResponse Response, Object o) throws Exception {
        logger.debug("开始验证会话");
        if(request.getSession()==null||request.getSession().getAttribute("username")==null){
            throw new ServiceException(Errcode.NO_SESSION);
        }
        return true;
    }

    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {
        logger.debug("会话验证完毕1");
    }

    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {
        logger.debug("会话验证完毕2");
    }
}
